/****************************************************************************
 * Chariott - Continuous Integration Testing Tool
 * Zaid Mohsin
 * The University of York
 * 2012/2013
 * MSc in Information Technology
 * All rights reserved.
 ****************************************************************************/
package chariott.testing;

import java.util.ArrayList;

/**
 * entity class that describes a single JUnit test case to be run.
 * 
 * @author Zaid
 * 
 */
public class TestCase implements RunItem {

	private ArrayList<TestMethod> myTestMethods;
	private String name;
	private String runType;
	private double weight;

	/**
	 * test case entity that encapsulate the name of the java test case and its
	 * estimated running time weight
	 * 
	 * @param name
	 * @param weight
	 */
	public TestCase(String name, double weight) {
		this.name = name;
		this.runType = "all";
		this.weight = weight;

		myTestMethods = new ArrayList();
	}

	public TestCase(String name) {
		this.name = name;
		this.runType = "method";

		myTestMethods = new ArrayList();
	}

	public TestCase(String name, ArrayList<TestMethod> myTestMethods) {
		this.name = name;
		this.myTestMethods = myTestMethods;
		this.runType = "method";

	}

	/**
	 * add a testcase method
	 * 
	 * @param myMethod
	 */
	public void addMethod(TestMethod myMethod) {
		myTestMethods.add(myMethod);
	}

	/**
	 * remove a test case method
	 * 
	 * @param myMethod
	 */
	public void removeMethod(TestMethod myMethod) {
		myTestMethods.remove(myMethod);
	}

	/**
	 * return all the test case methods
	 * 
	 * @return
	 */
	public ArrayList<TestMethod> getMethods() {
		return myTestMethods;
	}

	/**
	 * calculate the total weight of the test case as specified by the test
	 * configuration file, adds weights of single methods included in the test
	 * cases
	 * 
	 */
	public double getWeight() {
		double totalWeight = 0;

		if (runType.equals("all"))
			return weight;
		else {
			for (TestMethod testMethod : myTestMethods) {
				totalWeight = totalWeight + testMethod.getWeight();
			}

			return totalWeight;
		}
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		if (runType.equals("all"))
			return ("Test Case: " + name);
		else {
			sb.append("Test Case: " + name + "\n");
			for (TestMethod testMethod : myTestMethods) {
				sb.append(testMethod.toString() + "\n");
			}

			return sb.toString();

		}
	}

	public int numOfRuns() {

		if (runType.equals("all"))
			return 1;
		else {
			return myTestMethods.size();
		}
	}

	public String getRunType() {
		return runType;
	}

	/**
	 * returns the name of the test case
	 * 
	 */
	public String getName() {
		return name;
	}

	/**
	 * sets the weight of the test case
	 * 
	 * @param newWeight
	 */
	public void setWeight(double newWeight) {
		weight = newWeight;
	}
}
